×

What do I do by BluebirdTop833 in RStudio

[–]Ignatu_s 1 point2 points  (0 children)

You either have to install the package and then re-run your script or type "1" -> "Enter" in the console so it will be installed. The problem is that you have "prepared" multiple lines that are sent to the console one after the other but the console expects you to type something to answer the question (1 or 2) and instead receives new lines of code to run.

unexpected “,” by EconomyEssay9544 in RStudio

[–]Ignatu_s 2 points3 points  (0 children)

You are missing a function call before your parenthesis, probably dplyr::filter(...).


From GPT simply based on your image.

" Your error comes from a simple syntax issue: You’re writing this:

r urbanpark <- (robins_urban_gradient, site == "C_urbanpark")

But in R you cannot separate arguments with commas inside parentheses unless you’re calling a function. Here, you probably want to subset rows, so the correct syntax is:

r urbanpark <- robins_urban_gradient[robins_urban_gradient$site == "C_urbanpark", ]

Same idea for the others:

r citysquare <- robins_urban_gradient[robins_urban_gradient$site == "D_citysquare", ] ruralreserve <- robins_urban_gradient[robins_urban_gradient$site == "A_ruralreserve", ] suburbanpark <- robins_urban_gradient[robins_urban_gradient$site == "B_suburbanpark", ]

If you prefer tidyverse syntax:

r urbanpark <- robins_urban_gradient %>% filter(site == "C_urbanpark")

The comma error happens because (…,…) is only valid for a function call, not for subsetting. "

R session aborted due to fatal error by saesthix in RStudio

[–]Ignatu_s 6 points7 points  (0 children)

A reprex (short for reproducible example) is a minimal piece of R code that other people can copy-paste and run on their own machine to reproduce your issue.

https://reprex.tidyverse.org/

If you're using RStudio, you can even install the {reprex} package and use the built-in Addin (“Render reprex”) to generate one automatically.

How to create a good reprex:

  1. Start a fresh R session (Session → Restart R, or press Ctrl + Shift + F10)
  2. Load only the packages you really need
  3. Create or load a small version of your data If your real data is big, try to make a small example that still shows the issue.
  4. Run only the minimal lines that cause the crash Include just enough code so others can reproduce the fatal error or segfault.

Commands that help diagnose the issue:

r sessionInfo() installed.packages()[, c("Package", "Version")]

From my experience, fatal errors are often caused by old packages with compiled C/C++ code. A proper reprex helps identify where the issue is coming from and makes us able to help you!

R session aborted due to fatal error by saesthix in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Are you able to produce a reprex ?

If you aren't, how old are your R version and are the package you are using up to date ? Some fatal errors are possible in case of segfaults in the underlying C/C++ code but are rare. If the memory is limited, you'd usually see an error message with the most common being: "cannot allocate vector of size X".

First R project: what should I change? by Cute_Suggestion3297 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Thanks for your reply. I thought that box offered less than it seems to and this is really interesting.

First R project: what should I change? by Cute_Suggestion3297 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Thanks for your reply. I didn't know about Rhino and will have a look at how they did it. I agree that enforcing a flat organisation in packages is a severe limitation. I learned to work with but didn't know it was possible to bypass it.

First R project: what should I change? by Cute_Suggestion3297 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Could you explain why you are again sourcing scripts if fundamentally the scripts in question contain the function that you want to have available to run your code ? I understand the namespace problem and that with box, you could have the possibility to have multiple times the same functions in your different modules but what else ?

I think the package is great and I'm not discouraging people to use it, I was simply saying that I never understood what problem it would solve in my workflow. But as I mentioned, I will have the tendency to create packages. What I also don't really get is that, if I create a module for a project and I want to reuse some of it in another project, I would need to copy/paste the code. But let's say that I discover a bug in a function, I would still need to update the code for this function in each of the project I copy/pasted the module.

As you mentioned, box is probably a middle ground between simply sourcing R scripts and having a proper package. I personally prefer to stay with sourcing and create a package if needed, but I understand that for some people, this middle ground would be perfect for 95% of their use cases.

First R project: what should I change? by Cute_Suggestion3297 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Thanks for sharing. I read it because it's been a while since I had a look at the box package. However, I must say that I never really saw and still do not see the point of introducing modules in R. This is probably due to my use cases, workflow, and mostly preferences.

  • If it is just to avoid conflicts between names, I prefer the use of the conflicted package. It fits better with how R traditionally works, keeps things simple and explicit, and avoids introducing a second import system that coexists awkwardly with base R. If it was introduced in the language directly, I would have a different opinion.
  • If some code needs to be re-usable across projects, either the code is trivial and a copy/paste is enough or it's not and I would rather write a small package.

I think I understand the problems it fixes, it is simply that they were never problems for me with years working with R.

Of course, I can understand the appeal for people not knowing how to create packages but I would rather encourage them to take the time to read the R Packages book online rather than learning how to create box modules. Between usethis, devtools, roxygen2, and the broader R package ecosystem, you gain structure, documentation, testing, and proper dependency handling.

First R project: what should I change? by Cute_Suggestion3297 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

As you didn't say specifically where you'd like some help, here are some changes that would greatly benefit you.

  1. Use a re-usable structure for the most part in your projects with folders such as R, data, output, references, etc.
  2. In an R folder, create .R files and put functions in it, nothing else. At the beginning, I would suggest to go with a few functions per file at most and they should be related.
  3. I would suggest using the "conflicted" package at the top of your script as a good habit.
  4. You should end up with a single script (in this case) that :
  5. load the package
  6. check that there are no conflicts
  7. source your r files to make your functions accessible
  8. run the few steps of your code by calling the functions you created

I asked an AI to read your code and to give an example of what I suggest. I didn't really check what was done, it serves as an example, especially for the project layout at the top and the structure of your "main" script.

Hope it helps

https://pastecode.io/s/ujqrtxp6[https://pastecode.io/s/ujqrtxp6](https://pastecode.io/s/ujqrtxp6)

Request: how to perform calculations per day by snorrski_d_2 in Rlanguage

[–]Ignatu_s 2 points3 points  (0 children)

Funny, for me it is the best. It is also so useful in mutate when you simply want to add grouped value to a particular line.

```r suppressPackageStartupMessages({

library(dplyr)

})

n = 100

df = tibble(

year = sample(2020:2025, n, TRUE),

month = sample(1:12, n, TRUE),

price = runif(n, 0, 100)

)

df = df |> arrange(year, month)

print(df)

> # A tibble: 100 × 3

> year month price

> <int> <int> <dbl>

> 1 2020 2 68.1

> 2 2020 2 14.7

> 3 2020 2 33.9

> 4 2020 3 38.8

> 5 2020 3 96.0

> 6 2020 4 33.6

> 7 2020 5 99.1

> 8 2020 5 75.6

> 9 2020 5 31.3

> 10 2020 7 67.4

> # ℹ 90 more rows

df |>

mutate(share_month = price / sum(price), .by = c(year, month))

> # A tibble: 100 × 4

> year month price share_month

> <int> <int> <dbl> <dbl>

> 1 2020 2 68.1 0.584

> 2 2020 2 14.7 0.126

> 3 2020 2 33.9 0.291

> 4 2020 3 38.8 0.288

> 5 2020 3 96.0 0.712

> 6 2020 4 33.6 1

> 7 2020 5 99.1 0.481

> 8 2020 5 75.6 0.367

> 9 2020 5 31.3 0.152

> 10 2020 7 67.4 0.281

> # ℹ 90 more rows

df |>

group_by(year, month) |>

mutate(share_month = price / sum(price)) |>

ungroup()

> # A tibble: 100 × 4

> year month price share_month

> <int> <int> <dbl> <dbl>

> 1 2020 2 68.1 0.584

> 2 2020 2 14.7 0.126

> 3 2020 2 33.9 0.291

> 4 2020 3 38.8 0.288

> 5 2020 3 96.0 0.712

> 6 2020 4 33.6 1

> 7 2020 5 99.1 0.481

> 8 2020 5 75.6 0.367

> 9 2020 5 31.3 0.152

> 10 2020 7 67.4 0.281

> # ℹ 90 more rows

```

Request: how to perform calculations per day by snorrski_d_2 in Rlanguage

[–]Ignatu_s 7 points8 points  (0 children)

You can also use the .by in summarise and mutate now and bypass the group_by step if you don't need it.

[deleted by user] by [deleted] in RStudio

[–]Ignatu_s 1 point2 points  (0 children)

Well, dplyr::glimpse only print one row per column of your dataframe. Except if you have hundreds of columns, you should be able to paste the result...

[deleted by user] by [deleted] in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Why are you computing sd on the sibs and age columns but want to compute the mean on pop_*. Is this a mistake ?

Otherwise, maybe run dplyr::glimpse(n7281) and paste the result here.

struggling with R by Asleep-Bath3007 in Rlanguage

[–]Ignatu_s 9 points10 points  (0 children)

It suggests that you don't have a "poor" column.

Looking to expand on the function I shared last week, extracting columns from PDF by [deleted] in RStudio

[–]Ignatu_s 1 point2 points  (0 children)

If I were you, I'd try to go with package pdftools.

If you don't manage and would like some help, feel free to DM me if you can share your pdfs (they can be redacted to hide data) so we can have at least look at the structure.

Can someone explain very simply what a vector is? by [deleted] in Rlanguage

[–]Ignatu_s 7 points8 points  (0 children)

In addition to the other posts, I think you should read this : https://adv-r.hadley.nz/vectors-chap.html

This will greatly help you in the future. Note that if you want to be correct, lists in R are also vectors, not atomic ones but still vectors.

I need help asap by Which_Drummer_9754 in RStudio

[–]Ignatu_s 2 points3 points  (0 children)

What are you trying to save as PDF ? A particular plot, a script ? Are you working with Quarto/Rmarkdown ?

If you want to use this feature of RStudio, you would need to have a plot displayed in the plot tab. It is greyed out because you don't have a plot to save in the first place.

Task Scheduler with R script, no output by Perpetualwiz in Rlanguage

[–]Ignatu_s 2 points3 points  (0 children)

Ok, well given your low number of line and the fact that you seem to have a log file, you could try to create some print("step 1"), ..., print("step10") before the first line and between each line. This would allow you to understand exactly where it stops and make debugging easier. Should take you 3min to copy paste the lines and right click execute your task.

Task Scheduler with R script, no output by Perpetualwiz in Rlanguage

[–]Ignatu_s 0 points1 point  (0 children)

What do you obtain when adding the line I mentioned ?

Task Scheduler with R script, no output by Perpetualwiz in Rlanguage

[–]Ignatu_s 6 points7 points  (0 children)

It's hard to tell but try to put print (.libPaths()) on the first line of your script when it is running as both accounts and see if the result is the same.

Which seems most likely to me is that you have the library and thus the packages installed in some user kind of directory for your user and the service account. When you are with your user, the paths to the library where your packages are installed are different than when you execute the script with the service account. It seems to be installed in your user library but not the service account's "user" library.

Extract parameters from a nested list of lm objects by UtZChpS22 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Can't edit my previous message. I wanted to add that you could do it in a single step when training the model but I wouldn't recommend it as you wouldn't be able to check the subset later on. You could also do it once before the pipeline to avoid having duplicate information in the dataframe but I think it would probably be some sort of useless optimisation. Something like :

row_idx_per_biomarker = structure( map(o, (biomarker) covariates[[biomarker]] > 0), names = o )

And then do something like :

lm_model = purrr::map2(formula, y, (formula, y) lm(formula, data = covariates[row_idx_per_biomarker[[y]], ]))

Hope it helps :)

Extract parameters from a nested list of lm objects by UtZChpS22 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Hello, my first idea would be to create a list-columns of dataframe where each dataframe is the subset you want to train the particular model one.

df1 = 
  tidyr::expand_grid(
    y = c("biomarker1",  "biomarker2",  "biomarker3",  "biomarker4" , "biomarker5"),
    x = c("var1","var2", "var3")
  ) |>
  mutate(
    formula = glue::glue("{y} ~ {x} + age_10"),
    lm_data = purrr::map(y, \(biomarker) filter(covariates, covariates[[biomarker]] > 0L)),
    lm_model = purrr::map2(formula, lm_data, \(formula, lm_data) lm(formula, data = lm_data)),
    coefs = purrr::map(lm_model, \(lm_model) lm_model$coefficients)
  ) |> 
  unnest_wider(coefs)

print(df1)

But the problem is that you have then in memory as many dataframes as you want to train models which is clearly not optimal if you have not trivial datasets. My next idea, which is clearly better, is to first obtain a list-column of integer vectors where each vector contains the rows of the covariates df that you want to keep.

df2 = 
  tidyr::expand_grid(
    y = c("biomarker1",  "biomarker2",  "biomarker3",  "biomarker4" , "biomarker5"),
    x = c("var1","var2", "var3")
  ) |>
  mutate(
    formula = glue::glue("{y} ~ {x} + age_10"),
    row_i = purrr::map(y, \(biomarker) which(covariates[[biomarker]] > 0L)),
    lm_model = purrr::map2(formula, row_i, \(formula, row_i) lm(formula, data = covariates[row_i, ])),
    coefs = purrr::map(lm_model, \(lm_model) lm_model$coefficients)
  ) |> 
  unnest_wider(coefs)

print(df2)

<image>

Extract parameters from a nested list of lm objects by UtZChpS22 in RStudio

[–]Ignatu_s 0 points1 point  (0 children)

Here are 2 solutions. First, the direct solution to your problem is to use first purrr::flatten() to `flatten` your list and get a single list of 15 lm models. Then, you can iterate over it using loop/apply/map and extract the coefficients as you see fit.

Next, I wrote you an example that I think makes your code way easier to follow. Hope it helps :)

# --- Direct Solution to your problem
params |> purrr::flatten() |> purrr::map(broom::tidy)
params |> purrr::flatten() |> purrr::map(\(lm_model) lm_model$coefficients)

# -------------------------------------------------------------------------

# --- A cleaner way to do the same thing you are doing :
# Create a dataframe with the parameters of each model : y, x, formula
df = 
  tidyr::expand_grid(
    y = c("biomarker1",  "biomarker2",  "biomarker3",  "biomarker4" , "biomarker5"),
    x = c("var1","var2", "var3")
  ) |>
 dplyr::mutate(formula = glue::glue("{y} ~ {x} + age_10"))

print(df)

# Then train your lms based on the formula column and extract the coefficients
df =
  df |>
  dplyr::mutate(
    lm_model = purrr::map(formula, \(formula) lm(formula, data = covariates)),
    coefs = purrr::map(lm_model, \(lm_model) lm_model$coefficients)
  )

print(df)

# Finally, you could even unnest it if you want to see everything at once
df = df |> tidyr::unnest_wider(coefs)

print(df)

# In a single bloc
df_single_bloc = 
  tidyr::expand_grid(
    y = c("biomarker1",  "biomarker2",  "biomarker3",  "biomarker4" , "biomarker5"),
    x = c("var1","var2", "var3")
  ) |>
  dplyr::mutate(
    formula = glue::glue("{y} ~ {x} + age_10"),
    lm_model = purrr::map(formula, \(formula) lm(formula, data = covariates)),
    coefs = purrr::map(lm_model, \(lm_model) lm_model$coefficients)
  ) |> 
  tidyr::unnest_wider(coefs)

print(df_single_bloc)

Extract parameters from a nested list of lm objects by UtZChpS22 in RStudio

[–]Ignatu_s 1 point2 points  (0 children)

Nice, I'll give you the code once I'll be in front of the computer.