我正在进行个性调查,并试图将数据保存到Google表格中。
我首先创建了一个工作表,该工作表的第一行为零,并且具有所需的标题。我有代码显示output $ vaules中参与者的分数。我试图用它来保存到谷歌表,但这也不起作用。
我没有收到任何错误,但数据没有保存到初始化的Google工作表中。
library(shiny)
library(googlesheets)
shiny_token <- gs_auth()
saveRDS(shiny_token, "shiny_app_token.rds")
initial_sheet <- data.frame(open1 = 0, open2 = 0, consc1 =
0, consc2 = 0, extra1 = 0, extra2 = 0, agree1 = 0, agree2 =
0, neur1 = 0, neur2 = 0)
gdoc_name <- "big5"
ss <- gs_new(gdoc_name, input=initial_sheet)
sheetkey <- ss$sheet_key
sheetkey <- "xxxx" #actual key would be here
ss <- gs_key(sheetkey)
set.seed(3000)
xseq<-seq(1,7,.01)
densities <-dnorm(xseq, 4,1)
ui <- fluidPage(
table <- "responses",
titlePanel("Personality Traits"),
sidebarLayout(
sidebarPanel(
sliderInput("extra1", "I see myself as someone who is extraverted, enthusiastic.", min = 1, max = 7, value = 1),
sliderInput("agree1", "I see myself as someone who is critical, quarrelsome.",min = 1, max = 7,value = 1),
sliderInput("consc1", "I see myself as someone who is dependable, self-disciplined.",min = 1, max = 7, value = 1),
sliderInput("neur1", "I see myself as someone who is anxious, easily upset.",min = 1, max = 7, value = 1),
sliderInput("open1", "I see myself as someone who is open to new experiences, complex.", min = 1, max = 7, value = 1),
sliderInput("extra2", "I see myself as someone who is reserved, quiet.", min = 1, max = 7, value = 1),
sliderInput("agree2", "I see myself as someone who is sympathetic, warm.", min = 1, max = 7, value = 1),
sliderInput("consc2", "I see myself as someone who is disorganized, careless", min = 1, max = 7, value = 1),
sliderInput("neur2", "I see myself as someone who is calm, emotionally stable.", min = 1, max = 7, value = 1),
sliderInput("open2", "I see myself as someone who is conventional, uncreative.", min = 1, max = 7, value = 1),
actionButton("submit", "Submit")
),
mainPanel(
tableOutput("values")
))
)
server <- function(input, output) {
sliderValues <- reactive({
data.frame(Name = c("Openness","Conscientiousness","Extraversion", "Agreeableness", "Neuroticism"),
Value = as.character(c((input$open1 + (8-input$open2))/2 (input$consc1 + (8 - input$consc2))/2, (input$extra1 + (8 - input$extra2))/2,(input$agree2 + (8 - input$agree1))/2,(input$neur1 + (8 - input$neur2))/2)),stringsAsFactors = FALSE)
})
observeEvent(input$submit, {
output$values <- renderTable({
sliderValues()
})
observeEvent(input$save, {
open1 <- input$open1
open2 <- input$open2
consc1 <- input$consc1
consc2 <- input$consc2
extra1 <- input$extra1
extra2 <- input$extra2
agree1 <- input$agree1
agree2 <- input$agree2
neur1 <- input$neur1
neur2 <- input$neur1
gs_add_row(input = output$save)
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
今天早些时候发表评论后,我玩了我的代码,并用它来更新我的Google工作表。
在我的项目中,我想单击付款并将付款日期发送到数据表中的“付款”列(C5)。需要执行的次数与存在空单元格(未付费)的次数相同(因此为“ for”循环)。
我计算电子表格中的所有行(total_rows)和付费列中没有日期的行数(n_rows)。这些值为我提供了激活操作按钮后代码需要更新的空白单元格范围。
这已添加到应用程序的UI部分:
actionButton("payments", label = "EXECUTE PAYMENTS")
这到服务器:
sheet = function() {gs_title("Title of the googlesheet")}
historic = function(){ sheet() %>% gs_read(ws = "spreadsheet name")}
observeEvent(input$payments, {
total_rows = nrow(historic())
ndata = historic() %>% filter(is.na(paid))
n_rows = nrow(ndata)
if (total_rows == n_rows){n_rows = n_rows+1}
for (i in (total_rows - n_rows + 2) : (total_rows+1)) {
anchor_range = paste("R", i, "C5", sep = "", collapse = NULL)
sheet() %>% gs_edit_cells(ws = "spreadsheet name", input = Sys.Date(), anchor = anchor_range)
}
希望这对您有用。 J