0

I have set up a FlexDashboard in R and copied a code that runs without problems in R Script. However, when I run the document in R Markdown it returns an error message saying 'Error: Object NA not found'. Going step by step I was able to figure out which part of the code is responsible for the error message. But I was not able to figure out why that is.

for (k in 1:count_dfs){
  
  dataframe1 <- get(alldfs[k])
  current_name <- alldfs[k]
  dfs_list[[current_name]] <- dataframe1
  
  }

The variables contain the following information: count_dfs: 5L alldfs: chr [1:5] (ie the name of all 5 dataframes that were read from Excel earlier in the code)

My guess would be the get function, but I'm neither sure nor would I know how to substitute it.

Do you have any ideas how I can solve (or work around) this?

Edit (1):

I have gotten one step further. Markdown needs the name in the get function. Therefore this code works:

for (k in 1:count_dfs){
 
dataframe1 <- get("BNP") 
current_name <- "BNP" 
dfs_list[[current_name]] <- dataframe1 

dataframe1 <- get("Abn Amro") 
current_name <- "Abn Amro" 
dfs_list[[current_name]] <- dataframe1 

dataframe1 <- get("Banco Sabadell") 
current_name <- "Banco Sabadell" 
dfs_list[[current_name]] <- dataframe1 } 

But how can I loop through all dataframes then? Substituting the "BNP" by a variable doesn't work. Any ideas?

Florian
  • 5
  • 6
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). From the given information and code snippet one can only guess what's the issue. As you said that your code works fine when you run in a script the most likely reason is that you created an object interactively which is missing when you knit your flexdashboard. – stefan Nov 25 '22 at 08:26

1 Answers1

0

grrr, stupid error ...

I had this line of code:

assign(paste0(current_bank),current_data,envir = .GlobalEnv)

By changing this to:

assign(paste0(current_bank),current_data)

it worked fine.

Sorry for the incomplete question before.

Florian
  • 5
  • 6