Making knitr run a r script: do I use read_chunk or source?

前端 未结 3 1141
长情又很酷
长情又很酷 2020-12-24 07:54

I am running R version 2.15.3 with RStudio version 0.97.312. I have one script that reads my data from various sources and creates several data.tables. I then have another r

3条回答
  •  一生所求
    2020-12-24 08:31

    There isn't an option to run a chunk interactively from within knitr AFAIK. However, this can be done easily enough with something like:

    #' Run a previously loaded chunk interactively
    #'
    #' Takes labeled code loaded with load_chunk and runs it in the /global/ envir (unless otherwise specified)
    #'
    #' @param chunkName The name of the chunk as a character string
    #' @param envir The environment in which the chunk is to be evaluated 
    run_chunk <- function(chunkName,envir=.GlobalEnv) {
        chunkName <- unlist(lapply(as.list(substitute(.(chunkName)))[-1], as.character))    
        eval(parse(text=knitr:::knit_code$get(chunkName)),envir=envir) 
    } 
    NULL
    

提交回复
热议问题