我正在开发一个shinyapp,用于交互式地生成一个包含文件名称和一些新文本或注释的表格。
目前它按照以下步骤工作:
1.选择文件夹
2.使用文件名创建.csv(如果没有)
.csv的名称遵循模式“subfolderName”.csv
3.使用selectInput选项选择文件名
4.将每行(文件)的注释添加到.csv文件中
第二栏名为“评论”
我不能做的一些理想行为:
- 在文本字段(注释)中显示当前所选文件名(保存在.csv中)的上一个注释(如果有)。我也试过updateTextInput并且没有用。
#save as app.R
library(shiny)
library(shinyFiles)
library(shinyBS)
library(dplyr)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
shinyDirButton("dir", "1. Choose directory", "Upload")
,br(),br()
, bsButton("submit","2. Create or reset .csv", style="warning")
,br(),br()
, uiOutput("fileinput")
, uiOutput("uitextinput") # this is not showing existing comments
, bsButton("addname","4. Add comment", style="warning")
),
mainPanel(
h4("Selected folder"),
verbatimTextOutput("dirtext"), br(),
h4("Files in that dir"),
verbatimTextOutput("files"),
textOutput("result")
)
))
path1<-"~"
server <- function(input, output, session) {
output$result<-renderText({
t<- reac$message
})
shinyDirChoose(input, 'dir', roots = c(home = path1) )
reacdir <- reactive(input$dir)
output$dirtext <- renderPrint(c(path(),current() ) )
path <- reactive({
home <- normalizePath(path1)
file.path(home, paste(unlist(reacdir()$path[-1]), collapse = .Platform$file.sep))
})
current<-reactive({
a<-sub('.*\\/', '', path() )
b<-paste("current subdir:",a)
})
reac <- reactiveValues(comment=NULL, df2=NULL)
#not WORKING
output$uitextinput<-renderUI(
textInput("commentwritten","Comment for file", reac$comment)
)
# GET current .csv file with comments and load the comment to reac$comment , Not working
observe({
validate(
need(try(any(class(reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\\/', '', path() ),".csv") ,
read.csv()))=="data.frame")==TRUE), "Wait 801")
)
reac$df2<- reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\\/', '', path() ),".csv") , read.csv, stringsAsFactors=FALSE)
reac$comment<-reac$df2$comment[match(input$file, reac$df2$name)]
})
filedata<-reactive({
filenames<- list.files(path())
filedata<-data.frame(name=filenames)
})
# button 2. creating the csv file
observeEvent(input$submit,{
write.csv(filedata(), file =paste0(path(),"/",sub('.*\\/', '', path() ),".csv") , row.names = FALSE )
})
#step 3
output$fileinput<-renderUI(
div(style="width: 100%; margin: 0 0;",
selectInput("file", "3. Select filename to add details", filedata()$name)
)
)
#step 4, adding a comment to the .csv
observeEvent(input$addname , {
if(file.exists(paste0(path(),"/",sub('.*\\/', '', path() ),".csv")) ){
dfnameadded <- reactive({
originaldf<-reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\\/', '', path() ),".csv") , read.csv, stringsAsFactors=FALSE)
filetoadd <- data.frame(name=input$file , stringsAsFactors = FALSE)
filetoadd$comment<-input$commentwritten
l<-list(originaldf(),filetoadd)#
dfadded<- Reduce(bind_rows, l)
dfadded<-dfadded %>%
group_by(name) %>%
summarise(comment = paste0(na.omit(comment[length(comment)]), collapse = "; ") )
dfadded
})
tryCatch(write.csv(dfnameadded(), file =paste0(path(),"/",sub('.*\\/', '', path() ),".csv") , row.names = FALSE ),
error = function(e) {print(paste("create .csv first")) ; "create .csv first" } )
reac$message<-"comment added"
}
else
{
reac$message<-"create .csv first"
}
})
output$files <- renderPrint(list.files(path()))
} # end server
shinyApp(ui, server)
答案 0 :(得分:0)
我来到这个解决方案 替换
struct timeval t1, t2;
gettimeofday(&t1, NULL);
gettimeofday(&t2, NULL);
int milliSeconds = (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000;
使用:
#not WORKING
# output$uitextinput<-renderUI(
# textInput("commentwritten","Comment for file", reac$comment)
# )
答案 1 :(得分:-1)
试试这个:
reac <- reactiveValues(comment <- NULL, df <- NULL)
textInput("namewritten","Comment for photo",placeholder= if(is.null(reac$comment)){"No Comments"}else{reac$comment})
我也将df作为反应值。你使用一个观察基本上调用一个被动函数,但观察不会返回任何值,所以重新$ comment没有被返回。